package com.xiaoshuidi.cloud.framework.redis.config;

import com.xiaoshuidi.cloud.framework.redis.lock.DistributeLock;
import com.xiaoshuidi.cloud.framework.redis.sequence.TimebaseSequenceGenerator;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnClass({Redisson.class})
@AutoConfigureAfter(RedissonAutoConfiguration.class)
@Configuration
public class LeaseRedissonAutoConfiguration {
    @Autowired
    public void setRedissonClient(RedissonClient redissonClient) {
        DistributeLock.setRedissonClient(redissonClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public TimebaseSequenceGenerator timebaseSequenceGenerator(RedissonClient redissonClient) {
        return new TimebaseSequenceGenerator((key, n) -> redissonClient.getAtomicLong(key).addAndGet(n));
    }

}
